package com.dispatcher;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.alibaba.fastjson.JSON;
import com.dispatcher.bean.Gift;
import com.dispatcher.bean.Order;
import com.dispatcher.http.HttpHelperWraper;
import com.dispatcher.http.HttpHelperWraper.HttpRequestParam;
import com.dispatcher.http.HttpHelperWraper.OnNetListener;
import com.zuzhili.database.CacheDataCtrl;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

public class GiftActivity extends ActivityBase implements OnNetListener {
	private final String GET_GIFT_LIST = "getGiftList";
	private final String SEND_GIFT = "sendGift";
	
	private ListView lvGiftList;
	private Intent it;
	private List<Gift> giftList;
	private GiftAdapter adapter;
	private String giftName = null;
	private String giftId = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_gift);
		lvGiftList = (ListView) findViewById(R.id.lv_gift_list);
		
		it = getIntent();
		if(giftList==null){
			giftList = new ArrayList<Gift>();
			
			sendRequest(GET_GIFT_LIST);
		}else{
		}
		adapter = new GiftAdapter();
		lvGiftList.setAdapter(adapter);
	}
	
	void sendRequest(String action) {
		HttpHelperWraper http = new HttpHelperWraper();
		HttpRequestParam param = http.getParam();
		makeparam(param, action);
		http.AsyncTaskWithCache(param);
	}
	
	void makeparam(HttpRequestParam param, String action){
		if (param == null) {
			return;
		}
		param.ctx = this;
		param.listener = this;
		param.expiretime=0;
		
		HashMap<String, Object> requestparams = new HashMap<String, Object>();
		
		if(action.equals(GET_GIFT_LIST)) {
			param.activitybase=this;
			param.cachetype=CacheDataCtrl.CacheType.Gifts_Cache;
			param.identify="gifts";
			requestparams.put("task", "getGiftList");
			requestparams.put("begin", "0");
		} else if(action.equals(SEND_GIFT)) {
			requestparams.put("task", "sendGift");
			requestparams.put("orderid", it.getStringExtra("orderid"));
			requestparams.put("presentid", it.getStringExtra("presentid"));
			requestparams.put("giftid", giftId);
		}
		param.nodesrequest = requestparams;
	}
	
	@Override
	public void onNetSuccess(HttpRequestParam param) {
		Log.i("liutao", param.nodesrequest.toString());
//		Log.i("liutao", param.nodesresult.toString());
//		JSONObject nodesresult = param.nodesresult;
		String action = (String) param.nodesrequest.get("task");
		if(action != null && action.equals("getGiftList")) {
//			JSONArray jsonArray = null;
//			if(nodesresult.has("list")) {
//				try {
//					jsonArray = (JSONArray) nodesresult.get("list");
//				} catch (JSONException e) {
//					e.printStackTrace();
//				}
//			}
			
			giftList = JSON.parseArray(JSON.parseObject(param.jsonstr).getString("list"), Gift.class);
			
			adapter.notifyDataSetChanged();
			
		} else if(action != null && action.equals("sendGift")) {
			
			Intent data = new Intent();
			data.putExtra("giftid", giftId);
			data.putExtra("name", giftName);
			data.putExtra("orderid", it.getStringExtra("orderid"));
			setResult(RESULT_OK, data);
			finish();
		}
		
		
	}

	@Override
	public void onNetError(HttpRequestParam param) {
		Toast.makeText(this, param.rstmsg, 2000).show();
	}

	OnClickListener leftlis = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
		}
	};
	
	OnClickListener rightlis = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
		}
	};
	
	public class GiftAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return giftList.size();
		}

		@Override
		public Object getItem(int arg0) {
			return giftList.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			return arg0;
		}

		@Override
		public View getView(int arg0, View arg1, ViewGroup arg2) {
			final int pos = arg0;
			LayoutInflater mInflater = LayoutInflater.from(GiftActivity.this);
			View view = mInflater.inflate(R.layout.gift_item, null);
			Button btGift = (Button) view.findViewById(R.id.bt_gift);
			
			Gift gift = (Gift)giftList.get(arg0);
			btGift.setText(gift.getName());
			btGift.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					giftName = giftList.get(pos).getName();
					giftId = giftList.get(pos).getGiftid();
					sendRequest(SEND_GIFT);
				}
			});
			
			return view;
		}
		
	}
	
}
